package com.tjetc.service.impl;

import com.tjetc.dao.NclassesDao;
import com.tjetc.dao.NurseDao;
import com.tjetc.dao.impl.NclassesDaoImpl;
import com.tjetc.dao.impl.NurseDaoImpl;
import com.tjetc.domain.*;
import com.tjetc.service.NclassesService;
import com.tjetc.utils.PageInfo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NclassesServiceImpl implements NclassesService {
    private NclassesDao nclassesDao = new NclassesDaoImpl();
    private NurseDao nurseDao = new NurseDaoImpl();
    @Override
    public boolean addNur(Nclasses nclasses) {
        try {
            nclassesDao.addNur(nclasses);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int getNurCount(int nid) {
        try {
            return nclassesDao.getNurCount(nid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<NurClasses> listNurClasses(PageInfo<NurClasses> pageInfo) {
        try {
            List<NurClasses> list = nclassesDao.listNurClasses(pageInfo);
            for (NurClasses classes : list) {
                List<String> nNames = new ArrayList<>();
                String[] split = classes.getNids().split(",");
                for (String s : split) {
                    Nurse nurse = nurseDao.getById(Integer.valueOf(s));
                    nNames.add(nurse.getUsername());
                }
                classes.setNames(nNames);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount() {
        try {
            return nclassesDao.getCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Nclasses findByIdandDate(int id, String dayOfWeekName) {
        try {
            return nclassesDao.findByIdandDate(id,dayOfWeekName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean setqiandao(Nclasses nclasses) {
        try {
            return nclassesDao.setqiandao(nclasses)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
